<template>
  <div class="classify-root">
    <div class="search-bar">
      <wux-search-bar
        clear
        show-cancel
        :value="value"
        controlled
        placeholder="请输入商品名称"
        @change="onChange"
        @confirm="onConfirm"
        @clear="onClear"
      />
    </div>
    <wux-spin nested :spinning="contentSpinning">
      <div class="classify-container">
        <div class="classify-list">
          <wux-spin nested :spinning="leftSpinning">
            <ul class="type-list">
              <li
                v-for="(type,index) in types"
                :key="index"
                :class="'type-item '+ type.class"
                @click="getClassify(type,index)"
              >{{type.value}}</li>
            </ul>
          </wux-spin>
        </div>
        <div class="classify-items">
          <goodsList :goods="goods" :showStyle="false"></goodsList>
        </div>
      </div>
    </wux-spin>
  </div>
</template>

<script>
import goodsList from "@/components/goods/goodsList";
import typeConfig from "@/api/types";

export default {
  components: {
    goodsList
  },
  data() {
    return {
      value: "",
      types: [],
      leftSpinning: false,
      contentSpinning: false,
      goods: []
    };
  },
  onShow() {
    if (this.$store.state.cart.length === 0) {
      wx.removeTabBarBadge({
        index: 2
      })
    } else {
      wx.setTabBarBadge({
        index: 2,
        text: this.$store.state.cart.length + ''
      })
    }
  },
  mounted() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"]
    });
    this.getTypes();
  },
  methods: {
    onClear() {
      this.value = "";
    },
    onChange(e) {
      this.value = e.target.value;
    },
    onConfirm() {
      const self = this;
      this.contentSpinning = !this.contentSpinning;
      this.$http
        .request({
          url: typeConfig.queryCommodityByShopIdTypeId,
          method: "POST",
          body: {
            title: self.value,
            sId: this.$store.state.shop.id
          }
        })
        .then(res => {
          this.contentSpinning = !this.contentSpinning;
          this.goods = res.list;
        });
    },
    onBlur() {},
    getTypes() {
      this.types = [{ id: "", value: "所有商品", class: "active" }];
      this.leftSpinning = !this.leftSpinning;
      this.$http
        .request({
          url: typeConfig.getTypes,
          method: "POST"
        })
        .then(res => {
          res.list.forEach((item, index) => {
            let type = {};
            type.value = item.title;
            type.id = item.id;
            this.types.push(type);
          });
          this.queryCommodity(this.types[0], 0);
          this.leftSpinning = !this.leftSpinning;
        });
    },
    queryCommodity(type, index) {
      this.contentSpinning = !this.contentSpinning;
      this.types.forEach(element => {
        element.class = "";
      });
      type.class = "active";
      this.$http
        .request({
          url: typeConfig.queryCommodityByShopIdTypeId,
          method: "POST",
          body: {
            type: type.id,
            sId: this.$store.state.shop.id
          }
        })
        .then(res => {
          this.contentSpinning = !this.contentSpinning;
          this.goods = res.list;
        });
    },
    getClassify(type, index) {
      if (this.types[index].class === "active") {
        return false;
      }
      this.queryCommodity(type, index);
    }
  }
};
</script>
<style scoped lang="less">
.classify-root {
  display: flex;
  flex-direction: column;
  height: 100vh;
  .search-bar {
    width: 100%;
  }
  .classify-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    .classify-list {
      overflow: auto;
      width: 30%;
      height: 100%;
      .type-list {
        // background-color: #f8f8f8;
        list-style: none;
        .type-item {
          display: block;
          text-align: center;
          font-size: 14px;
          height: 40px;
          line-height: 40px;
          position: relative;
          &:after {
            content: " ";
            width: 100%;
            position: absolute;
            left: 0;
            bottom: 0;
            height: 1px;
            background-color: #efefef;
          }
        }
        .active {
          background-color: #fff;
          color: #C89F62;
          position: relative;
          &:before {
            content: " ";
            position: absolute;
            height: 100%;
            width: 2px;
            left: 0;
            top: 0;
            background-color: #C89F62;
          }
        }
      }
    }
    .classify-items {
      display: flex;
      height: 100%;
      overflow: auto;
      width: 100%;
      /deep/ .goods-container .goods-img{
        height: 115px;
      }
    }
  }
}
</style>